package com.goods.controller.business;

import com.github.pagehelper.Page;
import com.goods.business.service.BizProductCategoryService;
import com.goods.common.model.business.BizProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/business/productCategory")
public class CategoryController {

    @Autowired
    private BizProductCategoryService bizProductCategoryService;
    @GetMapping("categoryTree")
    public ResponseBean categoryTree(@RequestParam int pageNum, @RequestParam int pageSize){
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = bizProductCategoryService.selectPage(pageNum, pageSize);
        HashMap<String, Object> resultMap= new HashMap<>();
        resultMap.put("total",productCategoryTreeNodeVOS.size());
        resultMap.put("rows",productCategoryTreeNodeVOS);
        return ResponseBean.success(resultMap);
    }
    @GetMapping("getParentCategoryTree")
    public ResponseBean getParentCategoryTree(){
        List<ProductCategoryTreeNodeVO> parentCategoryTree=bizProductCategoryService.getParentCategoryTree();
        return ResponseBean.success(parentCategoryTree);
    }
    @PostMapping("add")
    public ResponseBean add(@RequestBody BizProductCategory bizProductCategory){
        bizProductCategoryService.add(bizProductCategory);
        return ResponseBean.success();
    }
    @GetMapping("edit/{id}")
    public ResponseBean edit(@PathVariable long id){
        BizProductCategory bizProductCategory=bizProductCategoryService.findById(id);
        return ResponseBean.success(bizProductCategory);
    }
    @PutMapping("update/{id}")
    public ResponseBean update(@RequestBody BizProductCategory bizProductCategory){
        bizProductCategoryService.update(bizProductCategory);
        return ResponseBean.success();
    }
    @DeleteMapping("delete/{id}")
    public ResponseBean delete(@PathVariable long id) throws Exception {
        bizProductCategoryService.deleteById(id);
        return ResponseBean.success();
    }
}
